/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.flexengine.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class Persister {
    
    private final PersisterWriter writer;
    private final PersisterReader reader;

    public Persister() {
        this.writer = new PersisterWriter();
        this.reader = new PersisterReader();
    }
    
    public void writer(Object obj, String file) throws Exception {
        writer.writer(obj, new File(file));
    }
    
    public void writer(Object obj, File   file) throws Exception {
        writer.writer(obj, new FileOutputStream(file));
    }
    
    public void writer(Object obj, OutputStream out) throws Exception {
        writer.writer(obj, out);
    }
    
    public <T> T read(Class<? extends T> clazz, String file) throws Exception {
        return reader.read(clazz, file);
    }
    public <T> T read(Class<? extends T> clazz, File file) throws Exception {
        return reader.read(clazz, file);
    }
    public <T> T read(Class<? extends T> clazz, InputStream in) throws Exception {
        return reader.read(clazz, in);
    }
    
    public static int countAttributs(Class clazz) {
        Field[] fields = clazz.getDeclaredFields();
        Method[] methods = clazz.getDeclaredMethods();
        for(Field f : fields) {
            if( f.getAnnotation(Attribute.class)!=null ) {
                return 1;
            }
        }
        for(Method m : methods) {
            if( m.getAnnotation(Attribute.class)!=null ) {
                return 1;
            }
        }
        return 0;
    }
    
    public static int countElements(Class clazz) {
        Field[] fields = clazz.getDeclaredFields();
        Method[] methods = clazz.getDeclaredMethods();
        for(Field f : fields) {
            if( f.getAnnotation(Element.class)!=null ) {
                return 1;
            }
        }
        for(Method m : methods) {
            if( m.getAnnotation(Element.class)!=null ) {
                return 1;
            }
        }
        return 0;
    }
    
    public static boolean isPrimitive(Object obj) {
        if( obj == null  ) {
            return false;
        }
        return obj instanceof String || obj instanceof Boolean || obj instanceof Integer   || 
               obj instanceof Float  || obj instanceof Double  || obj instanceof Character || 
               obj instanceof Byte   || obj instanceof Short   || obj instanceof Long;
    }
    
    public static boolean isPrimivite(Class type) {
        if( type == String.class ) {
            return true;
        }
        if( type == Boolean.class || type.getName().equals("boolean")) {
            return true;
        }
        if( type == Integer.class || type.getName().equals("int") ) {
            return true;
        }
        if( type == Float.class || type.getName().equals("float")) {
            return true;
        }
        if( type == Double.class || type.getName().equals("double")) {
            return true;
        }
        if( type == Character.class || type.getName().equals("char")) {
            return true;
        }
        if( type == Byte.class || type.getName().equals("byte")) {
            return true;
        }
        if( type == Short.class || type.getName().equals("short")) {
            return true;
        }
        return type == Long.class || type.getName().equals("long");
    }
    
}
